package com.tabbinder.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Panel containing header fields for Binder/user
 * 
 * @author Tatiana Kichkaylo
 */
public class HeaderPanel {
	private static String[] UNITS = { "days", "weeks", "months" };
	private static String NOT_SHARED = "Not shared";

	private TextBox titleBox;
	private CheckBox favoriteBox;
	private Label expiresLabel;
	private CheckBox expiresSet;
	private TextBox expiresDur;
	private ListBox expiresUnit;
	private Label sharedWith;
	private Label applyChanges;
	private Label deleteItem;
	
	private ListItem selectedItem = null;
	private MasterBind master;

	public HeaderPanel(MasterBind master) {
		this.master = master;

		titleBox = new TextBox();
		titleBox.addStyleName("titleField");
		RootPanel.get("titleField").add(titleBox);

		favoriteBox = new CheckBox("is favorite (never expires)");
		RootPanel.get("favoriteField").add(favoriteBox);

		expiresLabel = new Label();
		RootPanel.get("expiresLabel").add(expiresLabel);
		expiresSet = new CheckBox("reset to");
		RootPanel.get("expiresSet").add(expiresSet);
		expiresDur = new TextBox();
		RootPanel.get("expiresDur").add(expiresDur);
		expiresUnit = new ListBox();
		RootPanel.get("expiresUnits").add(expiresUnit);
		for (String s : UNITS)
			expiresUnit.addItem(s);

		sharedWith = new Label();
		RootPanel.get("sharedWith").add(sharedWith);

		RootPanel buttons = RootPanel.get("headerButtons");
		applyChanges = new Label("Apply changes");
		applyChanges.addStyleName("binderButton");
		buttons.add(applyChanges);
		deleteItem = new Label("Delete this binder");
		deleteItem.addStyleName("binderButton");
		buttons.add(deleteItem);
		
		selectItem(null);
		
		favoriteBox.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				setFavorite(favoriteBox.getValue());
			}});
		
		applyChanges.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (!applyChanges.getStyleName().contains("disabled"))
					updateSelectedItem();
			}});
		deleteItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (!deleteItem.getStyleName().contains("disabled"))
					deleteSelectedItem();
			}});
	}

	public void selectItem(ListItem item) {
		selectedItem  = item;
		
		if (item == null) {
			titleBox.setText("");
			titleBox.setEnabled(false);
			setFavorite(true);
			favoriteBox.setEnabled(false);
			sharedWith.setText(NOT_SHARED);
			applyChanges.addStyleName("disabled");
			deleteItem.addStyleName("disabled");
			return;
		}
		
		titleBox.setText(item.getBinderTitle());
		titleBox.setEnabled(true); 
		
		boolean fav = item.getFavorite();
		setFavorite(fav);
		favoriteBox.setEnabled(true);
		
		int rcount = item.getRecordCount();
		if (rcount == 1)
			sharedWith.setText(NOT_SHARED);
		else
			sharedWith.setText("Shared with " + (rcount - 1) + " other users");
		applyChanges.removeStyleName("disabled");
		deleteItem.removeStyleName("disabled");
	}
	
	private void setFavorite(boolean value) {
		favoriteBox.setValue(value);
		expiresSet.setValue(false);
		expiresDur.setText("");
		expiresDur.setEnabled(false);
		expiresUnit.setEnabled(false);
		
		expiresSet.setEnabled(!value);
		if (value) 
			expiresLabel.setText("Does not expire");
	}
	

	protected void updateSelectedItem() {
		// Paranoia
		if (selectedItem == null)
			return;
		selectedItem.setBinderTitle(titleBox.getText());
		selectedItem.setFavorite(favoriteBox.getValue());
		
		// Notify server
		master.submitUpdate(selectedItem);
	}
	
	protected void deleteSelectedItem() {
		// Paranoia
		if (selectedItem == null) 
			return;
		
		master.itemRemoved(selectedItem);
	}
}
